import { useNavigate } from 'react-router-dom'
import { Dialog } from 'antd-mobile'

import defaultAvatar from '@/assets/images/photo.png'  // 默认头像  
import { useLocalStorageState } from 'ahooks'
// 跳转
export const useComment = () => {
  const router = useNavigate()
  const gotoWhere = (url: string) => {
    router(url)
  }
  const [AppToken, setAppToken] = useLocalStorageState<string | undefined>(
    'AppToken',
  )
  const checkHashLogin = (next: any) => {
    if (AppToken) {
      next()
    } else {
      Dialog.show({
        content: '你还没有登录,请先登录哦',
        closeOnAction: true,
        actions: [
          [
            {
              key: 'cancel',
              text: '取消',
            },
            {
              key: 'delete',
              text: '去登录',
              bold: true,
              danger: true,
              onClick: () => {
                gotoWhere('/login')
              }
            },
          ],
        ],
      })
    }
  }

  const resetFields = (form: any) => {
    form.resetFields()
  }
  return {
    gotoWhere,
    defaultAvatar,
    checkHashLogin,
    resetFields
  }
}


// 判断是第几周
export const useGetYearWeek = () => {
  var time = new Date()
  var a = time.getFullYear()
  var b: string | number | any = time.getMonth() + 1;
  var c = time.getDate()

  var date1 = new Date(a, parseInt(b) - 1, c),
    date2 = new Date(a, 0, 1),
    d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
  return [
    Math.ceil((d + (date2.getDay() + 1 - 1)) / 7)
  ]
}


export const useGoBackOrMine = () => {
  const router = useNavigate()

  const GoBackOrMine = () => {
    let fromPath = localStorage.getItem('fromPath')
    if (fromPath == '/reg' || fromPath == '/findpass' || fromPath == '/changepass') {
      router('/main/mine')
    } else {
      router(-1)
    }
  }
  return {
    GoBackOrMine
  }
}
